import 'package:flustars_flutter3/flustars_flutter3.dart';
import 'package:flutter/material.dart';
import 'package:ls_work/a_yyt/page/human_visit_confirmed_page.dart';
import 'package:ls_work/a_yyt/page/human_visit_unconfirm_page.dart';
import 'package:ls_work/common/common.dart';
import 'package:ls_work/res/resources.dart';
import 'package:ls_work/util/event_bus.dart';
import 'package:ls_work/widgets/search_bar.dart' as e;

class HumanVisitHomePage extends StatefulWidget {
  @override
  _HumanVisitHomePageState createState() => _HumanVisitHomePageState();
}

class _HumanVisitHomePageState extends State<HumanVisitHomePage>
    with SingleTickerProviderStateMixin, AutomaticKeepAliveClientMixin {
  PageController _pageController = PageController(initialPage: 0);
  TabController? _tabController;
  String _keyword = "";
  int _index = 0;

  @override
  bool get wantKeepAlive => true;

  @override
  void initState() {
    super.initState();
    _tabController = new TabController(vsync: this, length: 2);
  }

  @override
  void dispose() {
    _tabController?.dispose();
    _pageController.dispose();
    eventBus.off(Constant.eventHumanVisitSeaarch);
    super.dispose();
  }

  _onPageChange(int index) {
    _tabController?.animateTo(index);
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Scaffold(
      backgroundColor: Colours.bg_color,
      appBar: AppBar(
        elevation: 0.5,
        title: TabBar(
          onTap: (index) {
            if (!mounted) {
              return;
            }
            _index = index;
            _pageController.jumpToPage(index);
          },
          isScrollable: false,
          controller: _tabController,
          labelStyle: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
          indicatorSize: TabBarIndicatorSize.label,
          unselectedLabelColor: Colours.text_gray,
          unselectedLabelStyle: TextStyles.textGray14,
          labelColor: Colours.text_dark,
          indicatorColor: Colours.app_main,
          indicatorPadding: EdgeInsets.only(bottom: 5),
          tabs: <Widget>[
            Tab(
              text: '待确认',
            ),
            Tab(
              text: '已确认',
            ),
          ],
        ),
        bottom: PreferredSize(
          preferredSize: Size(ScreenUtil.getScreenW(context), 64),
          child: Container(
            padding: EdgeInsets.only(bottom: 15),
            color: Colors.white,
            child: e.SearchBar(
              hintText: "请输入联系人姓名",
              isShowBackButton: false,
              onPressed: (text) {
                FocusScope.of(context).unfocus();
                _keyword = text;
                eventBus.emit(Constant.eventHumanVisitSeaarch,[_keyword,_index]);
              },
            ),
          ),
        ),
      ),
      body: PageView.builder(
        key: const Key('pageView'),
        itemCount: 2,
        onPageChanged: _onPageChange,
        controller: _pageController,
        itemBuilder: (BuildContext context, int index) {
          switch (index) {
            case 0:
              return HumanVisitUnConfirmPage();
              break;
            case 1:
              return HumanVisitConfirmedPage();
              break;
            default:
              return Container();
          }
        },
      ),
    );
  }
}
